package com.carlinfo.userscenter.users.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.carlinfo.common.dao.impl.BaseDaoImpl;
import com.carlinfo.common.util.PageInfoUtil;
import com.carlinfo.userscenter.users.dao.IAMenuDao;
import com.carlinfo.userscenter.users.pojo.AMenu;

/**
 * 菜单表操作的Dao实现类
 * 
 * 每一个Dao只需要修改
 * 增加,更新,的sql语句就可以了
 * 
 * @author wangshMac
 */
public class AMenuDaoImpl extends BaseDaoImpl<AMenu> implements IAMenuDao
{
	@Override
	public int saveOneDao(AMenu menu)
	{
		/* 添加的sql语句 */
		String sql = "INSERT INTO `a_menu`(`parentId`, `name`, `url`, `content`, `showStatus`, `leafStatus`, `status`, `createTime`, `updateTime`, `pubTime`)"
				+ " VALUES "
				+ "(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(menu.getParentId());
		paramsList.add(menu.getName());
		paramsList.add(menu.getUrl());
		paramsList.add(menu.getContent());
		paramsList.add(menu.getShowStatus());
		paramsList.add(menu.getLeafStatus());
		paramsList.add(menu.getStatus());
		paramsList.add(menu.getCreateTime());
		paramsList.add(menu.getUpdateTime());
		paramsList.add(menu.getPubTime());
		
		Map<String, Object> resultMap = new HashMap<String, Object>();
		/* 木有Id */
		int res = this.executeJDBC(sql, paramsList , resultMap);
		//this.logger.info("--key:{}--" , resultMap);
		/**
		 * 放的是int,转换的时候,直接变成字符串,再转
		 * 放的是String,值是int:"5",转换的时候,直接变成字符串,再转
		 */
		menu.setId(Integer.valueOf(resultMap.get("id") + ""));
		return res ;
	}

	@Override
	public int updateOneDao(AMenu menu)
	{
		/*------只关心SQL语句和条件-------*/
		String sql = "update `a_menu` set `parentId`= ?, `name`= ?, `url`= ?, `content`= ?, "
				+ "`showStatus`= ?, `leafStatus`= ?, `status`= ?, `createTime`= ?, `updateTime`= ?, `pubTime` = ?"
				+ " where `id` = ?" ; 
		List<Object> paramsList = new ArrayList<Object>();
		paramsList.add(menu.getParentId());
		paramsList.add(menu.getName());
		paramsList.add(menu.getUrl());
		paramsList.add(menu.getContent());
		paramsList.add(menu.getShowStatus());
		paramsList.add(menu.getLeafStatus());
		paramsList.add(menu.getStatus());
		paramsList.add(menu.getCreateTime());
		paramsList.add(menu.getUpdateTime());
		paramsList.add(menu.getPubTime());
		paramsList.add(menu.getId());
		
		return this.executeJDBC(sql, paramsList);
	}

	/**
	 * 根据id删除
	 * delete from a_menu where id = ? 
	 * delete from a_menu where name = ? 
	 */
	@Override
	public int deleteOneDao(Map<String, Object> condMap)
	{
		/* 存储条件的List;List里面的元素个数和?的个数一样 */
		List<Object> paramsList = new ArrayList<Object>();
		/*sql语句 */
		String sql = "delete from a_menu "; 
		if(condMap.get("id") != null && 
				!"".equalsIgnoreCase(condMap.get("id") + ""))
		{
			sql = sql + " where id = ? ";
			paramsList.add(condMap.get("id"));
		}else if(condMap.get("name") != null && 
				!"".equalsIgnoreCase(condMap.get("name") + ""))
		{
			sql = sql + " where name = ? ";
			paramsList.add(condMap.get("name"));
		}
		
		return this.executeJDBC(sql, paramsList);
	}
	
	/**
	 * select * from a_menu where id = 1 ; 
	 * select * from a_menu where name = 1 ; 
	 */
	@Override
	public AMenu selectOneDao(Map<String, Object> condMap)
	{
		/* 准备一个容器,这里面放的就是参数 */
		List<Object> paramsList = new ArrayList<Object>();
		String sql = "select * from a_menu " ;
		//condMap.put("id", "");
		if(!"".equalsIgnoreCase(condMap.get("id") + "")
			&& condMap.get("id") != null)
		{
			/* condMap中必放Id
			 * condMap放了id是空字符串"",
			 * condMap中压根就木有放id
			 * 
			 * select * from a_menu where id = 1 ;
			 * condMap.get("id") + "":如果Map中木有放id,condMap.get("id");返回值为null;
			 * null+空字符串是"null" 
			 *  */
			sql = sql + " where id = ? " ;
			/* 为占位符赋值 */
			paramsList.add(condMap.get("id"));
		}else if(!"".equalsIgnoreCase(condMap.get("name") + "")
				&& condMap.get("name") != null)
		{
			/* condMap中必放name
			 * condMap放了name是空字符串"",
			 * condMap中压根就木有放name
			 * select * from a_menu where name = 1 ;
			 *  */
			sql = sql + " where name = ? " ;
			paramsList.add(condMap.get("name"));
		}
		
		/* 调用 converFromDb先初始化pojo*/
		this.pojoObj = new AMenu() ; 
		/* 所有和JDBC相关的,直接放到父类中,
		 * 子类不准出现JDBC相关的代码
		 * this.converFromDb(rs):子类木有调用,父类调用了,而子类重写了,
		 * new的是子类,调用的是子类的方法
		 *  */
		return this.findOneJDBC(sql, paramsList);
	}
	
	/**
	 * 分页:
	 * ~想办法把SQL语句上,加上limit
	 * 
	 * ~如何根据condMap(存储的是查询条件)组成以下的sql语句呢?
关键字:		select * from a_menu where (name like '%0%' or capital like '%0%' or context like '%0%');
状态	:		select * from a_menu where status = 1 ; 
时间	:		select * from a_menu where (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
关键字+状态:	select * from a_menu where (name like '%0%' or capital like '%0%' or context like '%0%') and status = 1 ;
关键字+时间:	select * from a_menu where (name like '%0%' or capital like '%0%' or context like '%0%') and (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
时间+状态:		select * from a_menu where status = 1 and (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
关键字+状态+时间: select * from a_menu where (name like '%0%' or capital like '%0%' or context like '%0%') and status = 1 and (pubTime >= '2018-09-18 00:00:00' and pubTime < '2018-09-19 10:00:00');
啥也不选:		select * from a_dysnasty ; 
	 */
	@Override
	public List<AMenu> selectListDao(PageInfoUtil pageInfoUtil,Map<String, Object> condMap)
	{
		/* 准备一个存放参数的容器 */
		List<Object> paramsList = new ArrayList<Object>();
		
		/* sql语句 */
		String sql = "select * from a_menu where 1 = 1 " ; 
		if(condMap.get("keyword") != null 
				&& !"".equalsIgnoreCase(condMap.get("keyword") + ""))
		{
			/* condMap中放了keyword;并且不是空字符串 */
			sql = sql + " and (name like ? or context like ?)" ; 
			paramsList.add("%" + condMap.get("keyword") + "%");
			paramsList.add("%" + condMap.get("keyword") + "%");
		}
		
		/* 状态可以是整数也可以是字符串,不管放成啥,都变成字符串
		 * 因为字符串可以表示任何类型
		 * 5 === "5"
		 *  */
		if(condMap.get("status") != null 
				&& !"".equalsIgnoreCase(condMap.get("status") + ""))
		{
			/* condMap中放了keyword;并且不是空字符串放了状态 */
			sql += " and status = ? " ; 
			paramsList.add(condMap.get("status"));
		}
		
		/* 时间只用判断是不是Date类型就可以
		 * null intanceof Date:返回值是true还是false
		 *  */
		if(condMap.get("stDate") instanceof Date 
			&& condMap.get("edDate") instanceof Date)
		{
			/* condMap中放了keyword;并且不是空字符串放了状态 */
			sql += " and (pubTime >= ? and pubTime < ? ) " ; 
			paramsList.add(condMap.get("stDate"));
			paramsList.add(condMap.get("edDate"));
		}
		
		/* 排序 */
		if("idAsc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc";
		}else if("idAscNameDesc".equalsIgnoreCase(condMap.get("orderBy") + ""))
		{
			/* id:升序 */
			sql += " order by id asc, name desc";
		}else
		{
			/* 默认的效果 */
			sql += " order by pubTime desc";
		}
		
		/* 调用 converFromDb先初始化pojo*/
		this.pojoObj = new AMenu() ; 
		/* 分页:传PageInfo
		 * 不分页: 不传PageInfo*/
		if(pageInfoUtil != null)
		{
			/* 分页 */
			return this.findListByPage(pageInfoUtil, sql, paramsList);
		}
		/* 不分页 */
		return this.findList(sql, paramsList);
	}
	
}
